var co = require('co');
var seqModule = require('../../../routes/createModules');
var nodeExcel = require('excel-export');
var seq = new seqModule();
var util = require('../../../common/util');
var formidable = require('formidable');
var fs = require('fs');
var moment = require("moment");

//met
var liveController = {


    //获取评论
    getCommentListByType: function (req, res) {
        co(function () {
            var paramD=[];
            var params = {};
            var paramT = {};
            var page=req.query.page;
            var rows=req.query.rows;
            // params["item"] = req.query.item;
            // params["sort"] = req.query.sort;
            if(req.query.content!=undefined&&req.query.content!=""){
                var paramU = {};
                paramU["$like"]="%"+req.query.content+"%";
                params["content"]=paramU;
            }

            if(req.query.startTime !=undefined && req.query.startTime!=""){
                paramD.add({"createTime":{"$gt":req.query.startTime}});
            }
            if(req.query.endTime !=undefined && req.query.endTime!=""){
                paramD.add({"createTime":{"$lt":req.query.endTime}});
            }
            params["$and"]=paramD;
            params["titleId"] = req.query.titleId;
            var include = [{ model: seq.memberModule, attributes: ['name', 'hospitalName', 'hospitalOffices'] }];
            paramT.include = include;
            params["state"] = 1;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            if(page != undefined && rows !=undefined){
                paramT["offset"]=parseInt(page);
                paramT["limit"]=parseInt(rows);
            }
            seq.commentLogModule.findAndCountAll(paramT).then(function (result) {
                res.json({"success":true,"page":page,"total":result.count,"result":result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     * 根据 ID 导出数据
     * @param {*} req
     * @param {*} res
     */
    exportByID : function(req, res) {
        co(function () {
            var excelName = "commentExport";
            var params = {};
            var paramT = {};
            if(req.query.titleId!=null){
                params["titleId"] = req.query.titleId;
            }

            var include = [{ model: seq.memberModule, attributes: ['name', 'hospitalName', 'hospitalOffices'] }];
            paramT.include = include;
            params["state"] = 1;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            seq.commentLogModule.findAndCountAll(paramT).then(function (result) {
                var conf = {};
                conf.stylesXmlFile = "styles.xml";//注意：这个文件需要去我刚刚发的那个网址上下载一个下来
                conf.name = excelName;
                conf.cols = [
                    {
                        caption: '姓名',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '医院',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '内容',
                        type: 'string',
                        width: 25
                    }, {
                        caption: '评论时间',
                        type: 'string',
                        width: 10
                    }
                ];


                var vac = new Array();
                for (var i = 0; i < result.count; i++) {
                    var val = result.rows[i];
                    var temp = new Array();
                    if(val.member!=undefined&&val.member!=null){
                        temp[0] = val.member.name;
                        temp[1] = val.member.hospitalName;
                    }else{
                        temp[0] = "";
                        temp[1] = "";
                    }
                    temp[2] = val.content || "";
                    temp[3] = val.createTime || "";

                    vac.push(temp);

                }
                conf.rows = vac;//conf.rows只接受数组
                var results = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                console.log(excelName);
                res.setHeader("Content-Disposition", "attachment; filename=" + "commentExport.xlsx");
                res.end(results, 'binary');

            });
        }).catch(function (e) {
            res.json({'success': false, 'msg': '导出失败'});
            console.log(e);
        });

    },

    
    /**
     * 根据 ID 删除评论
     * @param {*} req
     * @param {*} res
     */
    deleteByID : function(req, res){
        var params = req.query.params;
        var arr = params.split(",");
        try{
            seq.commentLogModule.destroy({
                where: {
                    'id': [arr]
                }
            }).then(function(result){
                res.status(200).json({
                    message: "删除成功！",
                    success:true
                });
            })
        }catch(err) {
            res.status(500).json({
                message: "服务错误！",
                err:err
            });
        }
    }

}
module.exports = liveController;